clear all;
cd ..; cd ..; cd ..; cd ..;
load('code_main_model\6_implied_w_r\computation_results\data.mat','income_entre','income_worker','grid_income','nmkt');
cd 'code_figure_table\maintext\Figure 9_10\model';

T=11;

% income: year, mkt, income_grid
income=income_entre+income_worker;
income_economy=squeeze(sum(income,2));
gini1(1:T,1)=0;
prc10(1:T,1)=0;
prc50(1:T,1)=0;
for t=1:T
    gini1(t)=gini(1e10.*income_economy(t,:),grid_income,false);
    temp=cumsum(income_economy(t,:));
    prc10(t)=get_cond_mean_prc(income_economy(t,:),grid_income,temp,0.1);
    prc50(t)=get_cond_mean_prc(income_economy(t,:),grid_income,temp,0.5);
end
prc_bottom50=1-prc50;
income_gini=gini1;


std_acrossmkt(1:T,1)=0;
mean_income(1:T,1:nmkt)=0;
for t=1:T
    for mkt=1:nmkt
        mean_income(t,mkt)=dot(squeeze(income(t,mkt,:)),grid_income)/sum(squeeze(income(t,mkt,:)));
    end
    std_acrossmkt(t)=cal_std_normbymean_log(ones(1,nmkt),mean_income(t,:));
end
std_model=std_acrossmkt;


gini_withinmkt(1:T,1)=0;
gini_mktlevel(1:T,1:nmkt)=0;
for t=1:T
    for mkt=1:nmkt
        gini_mktlevel(t,mkt)=gini(1e10.*squeeze(income(t,mkt,:)),grid_income,false);
    end
    gini_withinmkt(t)=mean(gini_mktlevel(t,:));
end
gini_within_overall=gini_withinmkt;

save('result_baseline.mat','income_gini','prc10','prc50','std_model','gini_within_overall')
% save('result_credit.mat','income_gini','prc10','prc50','std_model','gini_within_overall')
% save('result_deposit.mat','income_gini','prc10','prc50','std_model','gini_within_overall')
